package com.abel.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.abel.datamodel.type.Applicants;
import com.abel.domain.Person;
import com.abel.domain.SearchCriteria;
import com.abel.domain.types.PersonType;
import com.abel.repository.SearchRepository;
import com.abel.repository.exception.RepositoryException;
import com.abel.repository.helper.DataConverter;
import com.abel.repository.helper.QueryUtil;
import com.abel.util.DomainCreator;

public class SearchRepositoryImpl extends BaseRepositoryImpl implements SearchRepository {

	protected final Log logger = LogFactory.getLog(getClass());
	
	@Override
	public List<Person> getApplications(SearchCriteria criteria)
			throws RepositoryException {
        
		logger.debug("@SearchRepositoryImpl : addPersonalInformation : entered");

		Session session = null;
		Transaction tx = null;
		
		try {
			session = this.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();

			Criteria sessionCriteria = session.createCriteria(Applicants.class);
			if (criteria.getLastName() != null 
					&& !criteria.getLastName().equals("")) {
				sessionCriteria.add(Restrictions.ilike("aplLastMame",QueryUtil.formatLike(criteria.getLastName())));
			}
			
			if (criteria.getSsn() != null 
					&& !criteria.getSsn().equals("")) {
				sessionCriteria.add(Restrictions.ilike("aplSsn", QueryUtil.formatLike(criteria.getSsn())));
			}
			
			if (criteria.getDateSubmittedBegin() != null) {
				sessionCriteria.add(Restrictions.ge("aplCreateDts", criteria.getDateSubmittedBegin()));
			}
			
			if (criteria.getDateSubmittedEnd() != null) {
				sessionCriteria.add(Restrictions.le("aplCreateDts", criteria.getDateSubmittedEnd()));
			}
			
			List<Applicants> list =  sessionCriteria.list();
			
			//converting data objects to domain
			List<Person> persons = new ArrayList<Person>();
			DataConverter converter = new DataConverter();
			for (Applicants applicants : list) {
				
				Person person = DomainCreator.createPerson(PersonType.User);
				person.setUserId(applicants.getUserId());
				person.getApplication().setApplicationId(applicants.getAplId());
				person.getApplication().setPersonProfile(converter.convertApplicantsToPersonProfile(applicants));
				persons.add(person);
			}
			tx.commit();
			return persons;
		}
		catch (Exception e) {
			logger.info("@SearchRepositoryImpl : addPersonalInformation : exception - while searching" + e);
			e.printStackTrace();
			if (tx != null && tx.isActive()) {
				
				try {
				
					tx.rollback();
				
				} 
				catch (HibernateException he) {
					logger.debug("@SearchRepositoryImpl : addEducation : exception while rolling back search" + he);
					he.printStackTrace();
				}
			}

			throw new RepositoryException("Exception while searching for records",e);
		}

	}
	
	
	private void convertApplicantsToPerson() {
		
	}

}
